package com.hzit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzit.dto.StudentDTO;
import com.hzit.entity.Classes;
import com.hzit.entity.PageResult;
import com.hzit.entity.Student;
import com.hzit.entity.StudentExample;
import com.hzit.mapper.ClassesMapper;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.IStudentService;
import com.hzit.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service        // 一般用在业务逻辑层
//@Repository     // 一般用在dao层，这里就是mapper层
//@Controller     // 一般用在控制层
//@Component      // 一般用在不能确定是哪个层的组件
public class StudentServiceImpl implements IStudentService {
	@Autowired
	private StudentMapper studentMapper;
	@Autowired
	private ClassesMapper classesMapper;
	//1. 查询所有学生
	@Override
	public List<StudentDTO> findAll() {
		//1.1 查询出所有学生
		List<Student> students = studentMapper.selectByExample(null);
		//1.2 将上面的List<Student>转换为List<StudentDTO>集合
		List<StudentDTO> collect = students.stream().map(this::getStudentDTO).collect(Collectors.toList());
		//1.4 返回带有班级名称的学生列表
		return collect;
	}


	//2. 条件查询带分页
	@Override
	public PageResult<StudentDTO> search(int page, int pageSize, StudentVo studentVo) {
		//2.1 开始分页
		PageHelper.startPage(page,pageSize);
		//2.2 开始查询
		StudentExample example = new StudentExample();
		StudentExample.Criteria criteria = example.createCriteria();
		//2.2.1 构造查询条件
		if(studentVo != null){
			if(StrUtil.isNotBlank(studentVo.getSname())){
				criteria.andSnameLike("%" + studentVo.getSname() + "%");  // and sname like '%xxx%'
			}
			if(StrUtil.isNotBlank(studentVo.getAddr())){
				criteria.andAddrLike("%" + studentVo.getAddr() + "%");     // and addr like '%xxx%'
			}
			if(studentVo.getCid() != null && studentVo.getCid() != 0){
				criteria.andCidEqualTo(studentVo.getCid());                     // and cid = xxx
			}
		}
		//2.2.2 根据查询条件进行查询
		List<Student> students = studentMapper.selectByExample(example);
		//2.2.3 转换为Page<Student>对象
		Page<Student> studentPage = (Page<Student>) students;
		//2.2.4 得到每页的结果集对象
		List<Student> result = studentPage.getResult();
		//2.2.5 将上面的List<Student>转换为List<StudentDTO>对象
		List<StudentDTO> collect = result.stream().map(this::getStudentDTO).collect(Collectors.toList());
		//2.3 最后，返回PageResult对象
		return new PageResult<>(collect,studentPage.getTotal());
	}

	//3. 添加学生
	@Override
	public void add(StudentDTO dto) {
		Student student = getStudent(dto);
		studentMapper.insert(student);
	}

	//4. 修改学生
	@Override
	public void update(StudentDTO dto) {
		Student student = getStudent(dto);
		studentMapper.updateByPrimaryKey(student);
	}

	// 5. 删除学生
	@Override
	public void delete(String sids) {
		String[] split = sids.split(",");
		for (String sid : split) {
			studentMapper.deleteByPrimaryKey(new Integer(sid));
		}
	}

	// 将studentDto转换为student
	private Student getStudent(StudentDTO dto) {
		Student student = new Student();
		BeanUtil.copyProperties(dto,student,true);
		return student;
	}

	// 将student对象转换为dto对象
	private StudentDTO getStudentDTO(Student student) {
		StudentDTO dto = new StudentDTO();
		BeanUtil.copyProperties(student, dto, true);
		//1.3 设置班级名称
		Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
		dto.setCname(classes.getCname());
		return dto;
	}
}
